package com.example.demoflink;

import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.hutool.core.util.RandomUtil;
import com.example.demoflink.tools.RedisUtil;
import com.github.javafaker.Faker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class SaticScheduleTask {
    @Autowired
    private RedisUtil redisUtil;

    //3.添加定时任务
    @Scheduled(cron = "0/5 * * * * ?")
    //或直接指定时间间隔，例如：5秒
    //@Scheduled(fixedRate=5000)
    private void configureTasks() {
        String name = ChineseNameGenerator.getInstance().generate();
        int score = RandomUtil.randomInt(0, 10000);

        System.err.println("执行静态定时任务时间: " + LocalDateTime.now() + "，学生姓名：" + name + ",成绩：" + score);
        redisUtil.hset("zhang", name, score);
    }

    public static class Person {
        public String name;
        public Integer age;

        public Person() {
        }

        public Person(String name, Integer age) {
            this.name = name;
            this.age = age;
        }

        @Override
        public String toString() {
            return this.name.toString() + ": age " + this.age.toString();
        }
    }
}